package cn.wostore.wxhelper.adapter;

import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import cn.wostore.wxhelper.R;
import cn.wostore.wxhelper.bean.SelectUser;
import cn.wostore.wxhelper.bean.User;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuch
 * @date 2019/1/3 0003
 */
public class MasterAdapter extends RecyclerView.Adapter<MasterAdapter.VH> implements Filterable {
    private List<SelectUser> users;
    private List<SelectUser> filterUsers;
    private SelectUser selectedUser;
    private OnItemClickListener onItemClickListener;

    public MasterAdapter(List<User> users) {
        initUsers(users);
    }

    private void initUsers(List<User> users) {
        this.users = new ArrayList<>();
        this.filterUsers = new ArrayList<>();
        for (int i = 0; i < users.size(); i++) {
            this.users.add(new SelectUser(users.get(i), false, i));
            this.filterUsers.add(new SelectUser(users.get(i), false, i));
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setSelection(int position) {
        if (filterUsers.get(position).isSelected()) {
            for (SelectUser selectUser : users) {
                selectUser.setSelected(false);
            }
            filterUsers.get(position).setSelected(false);
            selectedUser = null;
        } else {
            for (SelectUser selectUser : filterUsers) {
                selectUser.setSelected(false);
            }
            filterUsers.get(position).setSelected(true);
            for (SelectUser selectUser : users) {
                selectUser.setSelected(false);
                if(selectUser.getIndex()==filterUsers.get(position).getIndex())
                    selectUser.setSelected(true);
            }
            selectedUser = filterUsers.get(position);
        }
        notifyDataSetChanged();
    }

    public int getSelect(){
        return selectedUser.getIndex();
    }

    public User getSelectUser() {
        if (selectedUser != null)
            return selectedUser.getUser();
        else
            return null;
    }

    @NonNull
    @Override
    public MasterAdapter.VH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.select_item, null);
        return new MasterAdapter.VH(v);
    }

    @Override
    public void onBindViewHolder(@NonNull final MasterAdapter.VH vh, int i) {
        //Picasso.get().load(Uri.parse(filterUser.get(i).getImgUrl())).tag(vh).into(vh.imgPhoto);
        vh.nameTextView.setText(filterUsers.get(i).getUser().getUserName());
        vh.stateCheckBox.setChecked(filterUsers.get(i).isSelected());
        vh.itemView.setSelected(filterUsers.get(i).isSelected());
        if (filterUsers.get(i).isSelected())
            vh.nameTextView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        else
            vh.nameTextView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));

        if (onItemClickListener != null) {
            vh.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(vh.itemView, vh.getAdapterPosition());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return filterUsers.size();
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                String charString = constraint.toString();
                if (charString.isEmpty()) {
                    filterUsers = users;
                } else {
                    List<SelectUser> filteredList = new ArrayList<>();
                    for (int i = 0; i < users.size(); i++) {
                        if (users.get(i).getUser().getUserName().contains(charString)) {
                            filteredList.add(users.get(i));
                        }
                    }
                    filterUsers = filteredList;
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = filterUsers;
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                filterUsers = (List<SelectUser>) results.values;
                notifyDataSetChanged();
            }
        };
    }

    static class VH extends RecyclerView.ViewHolder {
        ImageView imgPhoto;
        TextView nameTextView;
        CheckBox stateCheckBox;


        VH(View itemView) {
            super(itemView);
            imgPhoto = itemView.findViewById(R.id.iv_photo);
            nameTextView = itemView.findViewById(R.id.tv_name);
            stateCheckBox = itemView.findViewById(R.id.cb_state);
        }
    }
}
